package edu.ncu.controller;

import edu.ncu.entity.Photo;
import edu.ncu.entity.User;
import edu.ncu.service.PhotoService;
import edu.ncu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/photo")
public class PhotoController {
    final PhotoService photoService;
    final UserService userService;

    public PhotoController(PhotoService photoService, UserService userService) {
        this.photoService = photoService;
        this.userService = userService;
    }
    @RequestMapping("/")
    public String photos() {
        return "photosPage" ;
    }

    /**
     * 相册上传图片的功能
     * @param request
     * @param upload
     * @return
     * @throws IOException
     */
    @RequestMapping("/fileupload")
    public String fileupload(HttpServletRequest request, MultipartFile[] upload) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        if (upload == null) {
            System.out.println("请先选择图片");
        } else {
            // 先获取到要上传的文件目录
            String path = request.getSession().getServletContext().getRealPath("/upload");
            // 创建File对象，一会向该路径下上传文件
            File file = new File(path);
            // 判断路径是否存在，如果不存在，创建该路径
            if(!file.exists())
            {
                file.mkdirs();
            }
            // 依次获取到上传文件的名称并记录下来
            List<String> list = new ArrayList<>();
            for (MultipartFile multipartFile : upload)
            {
                if (!multipartFile.isEmpty())
                {
                    String filename = multipartFile.getOriginalFilename();
                    System.out.println(path + "-----" + filename);
                    // 上传文件
                    multipartFile.transferTo(new File(file, filename));
                    list.add(filename);
                    //存入数据库
                    Photo photo = new Photo();
                    photo.setLikeNum(0);
                    photo.setCommentNum(0);
                    photo.setPhotoName(filename);
                    System.out.println("上传文件获取到的user：" + request.getSession().getAttribute("user"));

                    photo.setUserId(user.getUserId());
                    photoService.savePhoto(photo);
                }
            }
        }
//        model.addAttribute("photoNames",list);
        return "redirect:/photo/findByUserId?userId="+ user.getUserId();
    }

    @RequestMapping("/findAll")
    public String findAll(Model model)
    {
        System.out.println("表现层:findAll方法执行了...");
        //调用service方法,需要service层的对象,需要注入
        List<Photo> photos = photoService.findAll();
        System.out.println(photos);
        return "photos";
    }

    @RequestMapping("/findByUserId")
    public String findByUserId(@RequestParam("userId") int userId, ModelMap modelMap)
    {
        List<Photo> photos = photoService.findByUserId(userId);
        System.out.println("查出userId为 "+userId+" 的相册："+photos);
        User byUserId = userService.findByUserId(userId);
        modelMap.put("user", byUserId);
        modelMap.put("photos", photos);
        return "/photosPage";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam("photoId") int photoId, HttpServletRequest request)
    {
        System.out.println("查出要删除的照片photoId为 "+photoId);
        photoService.deletePhoto(photoId);
        User user = (User)request.getSession().getAttribute("user");
        System.out.println("此时登录用户为"+user);
        return "redirect:/photo/findByUserId?userId="+ user.getUserId();
    }

}
